from flask import make_response

from apps import dbUtil
from apps.path import pathService


def analysisPathID(pathId):  # 返回时间可用的pathID，拖车ID，时间类型字符串
    trailerStr = pathId[14:]
    timeStr = pathId[:14]
    return timeStr, trailerStr, analysisPathIDToTimeType(timeStr)


def analysisPathIDToTimeType(timeStr):
    year = timeStr[:4]  # 年
    month = timeStr[4: 6]  # 月
    day = timeStr[6: 8]  # 日
    hour = timeStr[8: 10]  # 时
    minute = timeStr[10: 12]  # 分
    second = timeStr[12: 14]  # 秒
    return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second


def recordGpsAndPath(gps1, gps2, path_ID):
    db = dbUtil.getDbConnection()
    cursor = db.cursor()
    cursor.execute('INSERT INTO gps(longitude,latitude,path_ID) VALUES("' + str(gps1) + '","' + str(gps2) + '","' + str(
        path_ID) + '");')
    db.commit()
    cursor.close()
    db.close()
    insertPath(path_ID)


# 获得当前最大的GPSId值，可以认为是当前的点
def getCurrentGpsMax(pathID):
    db = dbUtil.getDbConnection()
    cursor = db.cursor()
    cursor.execute('SELECT MAX(GPS_ID) FROM gps where path_ID="' + pathID + '";')
    db.commit()
    data = cursor.fetchall()
    cursor.close()
    db.close()
    return data[0][0]


def pathIsExist(path_ID):
    db = dbUtil.getDbConnection()
    # 使用cursor()方法创建一个游标对象
    cursor = db.cursor()

    # 使用execute()方法执行SQL语句
    cursor.execute('SELECT * FROM path WHERE path_ID = "' + str(path_ID) + '";')

    # 使用fetall()获取全部数据
    data = cursor.fetchall()
    cursor.close()
    db.close()
    return len(data) != 0  # 返回这条path是否存在


# 对已经存在的GPS点 但是路径还没插入的 进行路径插入
def insertPath(path_ID):
    timeStr, trailer_ID, timeTypeStr = analysisPathID(path_ID)
    if not pathIsExist(path_ID):  # 如果路径不存在 才插入路径
        cur_GPS = getCurrentGpsMax(path_ID)
        db = dbUtil.getDbConnection()
        # 使用cursor()方法创建一个游标对象
        cursor = db.cursor()
        # 使用execute()方法执行SQL语句
        cursor.execute('INSERT INTO path( path_ID, start_position, end_position, start_time, trailer_ID) VALUES("' + str(
            path_ID) + '",' + str(cur_GPS) + ',' + str(cur_GPS) + ',"' + timeTypeStr + '","' +
            trailer_ID + '");')

        # 使用fetall()获取全部数据
        db.commit()
        data = cursor.fetchall()
        cursor.close()
        db.close()
        return len(data) != 0  # 返回这条path是否存在


def updatePath(path_ID, end_position, total_time):
    db = dbUtil.getDbConnection()
    # 使用cursor()方法创建一个游标对象
    cursor = db.cursor()

    # 使用execute()方法执行SQL语句
    cursor.execute(
        'UPDATE end_position=' + str(end_position) + ', total_time=' + str(total_time) + ' WHERE path_ID=' + str(
            path_ID) + ';')

    cursor.close()
    db.close()

def returnGpsByPathId(path_ID):

    db = dbUtil.getDbConnection()
    # 使用cursor()方法创建一个游标对象
    cursor = db.cursor()

    # 使用execute()方法执行SQL语句
    cursor.execute(
        'select longitude,latitude from gps where path_ID ="'+str(path_ID)+'";')

    cursor.close()
    db.close()
    data = cursor.fetchall()
    if len(data) == 0:
        return []
    else:
        path = []
        for temp in data:
            path.append((temp[0], temp[1]))
        return data


def deal_cors(res):
    # res.headers['Access-Control-Allow-Origin'] = '*'
    # res.headers['Access-Control-Allow-Methods'] = 'OPTIONS,HEAD,GET,POST'
    # res.headers['Access-Control-Allow-Headers'] = 'x-requested-with'

    res.headers['Access-Control-Allow-Origin'] = '*'
    res.headers['Access-Control-Allow-Method'] = '*'
    res.headers['Access-Control-Allow-Headers'] = '*'
    return res


if __name__ == '__main__':
    # getCurrentGpsMax("2")
    # str = '20211219104131trailer001'

    # recordGpsAndPath('12.3', '13.4', '20211219104131trailer001')
    returnGpsByPathId(1)
